package com.js.web.controller;

import com.js.core.entity.result.EditorResult;
import com.js.core.entity.result.ResultBean;
import com.js.core.service.FileService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @ClassName FileConrroller
 * @Description TODO
 * @Author mazp
 * @Date 2025/10/16 11:55
 */
@RequestMapping("/file")
@RestController
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 封面上传
     * @param file
     * @return
     */
    @RequestMapping("/uploadCover")
    public ResultBean uploadCover(MultipartFile file){
        return fileService.upload(file, "文章封面上传");
    }

    /**
     * 图片上传 (适配 WangEditor
     * @param file
     * @return
     */
    @RequestMapping("/uploadImage")
    public EditorResult uploadImage(@RequestParam("js-upload-image")MultipartFile file){
        EditorResult editorResult = new EditorResult();
        EditorResult.Data data = new EditorResult.Data();
        ResultBean resultBean = fileService.upload(file, "文章图片上传");
        if(resultBean.isSuccess()){
            Object url = resultBean.getData();
            data.setUrl(url.toString());
            editorResult.setData(data);
            editorResult.setErrno(0);
        }else {
            editorResult.setErrno(1);
        }
        return editorResult;
    }
}
